package com.agree.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.agree.common.ReturnResult;
import com.agree.consts.ErrorCode;
import com.agree.service.CommonService;
import com.alibaba.fastjson.JSONObject;
/**
 * 调用平台服务交易接口的通用接口
 * @author Administrator
 */
@Controller
@RequestMapping("/api")
public class CommonResource {

	@Autowired
	private CommonService commonService;
	
	private final String SYSTEM_ERROR = "服务器繁忙，请稍后重试";
	
	/**
	 * 通用的请求方法
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sendCommonPostRequest",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<ReturnResult> sendCommonPostRequest(HttpServletRequest request) {
		//获取所有请求参数
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, String> map = new HashMap<String, String>();
		Set<Entry<String,String[]>> entrySet = parameterMap.entrySet();
		for (Entry<String, String[]> entry : entrySet) {
			String key = entry.getKey();
			String[] valueArr = entry.getValue();
			//value值可能为空并且过滤一些不要的参数
			if(valueArr.length>0 && !"cacheBuster".equals(key) && !"crl_method".equals(key)) {
				map.put(key, valueArr[0]);
			}
		}
		ReturnResult result = commonService.updateCommonPostRequest(map);
		if(result.isSuccess()) {
			return ResponseEntity.ok(result);
		} else if(ErrorCode.PBE999.errorCode().equals(result.getErrorCode())) {
			//若是系统异常
			result.setErrorMsg(SYSTEM_ERROR);
			return new ResponseEntity<ReturnResult>(result, HttpStatus.INTERNAL_SERVER_ERROR);
		}
		//请求不成功
		return new ResponseEntity<ReturnResult>(result, HttpStatus.BAD_REQUEST);
		
	}
	
	/**
	 * 通用的请求方法
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/sendCommonRequest",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<ReturnResult> sendCommonRequest(HttpServletRequest request) {
		//获取所有请求参数
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, String> map = new HashMap<String, String>();
		Set<Entry<String,String[]>> entrySet = parameterMap.entrySet();
		for (Entry<String, String[]> entry : entrySet) {
			String key = entry.getKey();
			String[] valueArr = entry.getValue();
			System.out.println("============="+key+"::"+valueArr[0]+"==============");
			//value值可能为空并且过滤一些不要的参数
			if(valueArr.length>0 && !"cacheBuster".equals(key) && !"crl_method".equals(key)) {
				map.put(key, valueArr[0]);
			}
		}
		ReturnResult result = commonService.updateCommonRequest(map);
		if(result.isSuccess()) {
			return ResponseEntity.ok(result);
		} else if(ErrorCode.PBE999.errorCode().equals(result.getErrorCode())) {
			//若是系统异常
			result.setErrorMsg(SYSTEM_ERROR);
			return new ResponseEntity<ReturnResult>(result, HttpStatus.INTERNAL_SERVER_ERROR);
		}
		//请求不成功
		return new ResponseEntity<ReturnResult>(result, HttpStatus.BAD_REQUEST);
		
	}
	/**
	 * 通用的请求方法,参数在body中
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/sendCommonPostBodyRequest",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<ReturnResult> sendCommonBodyRequest(@RequestBody String body) {
		//获取所有请求参数
		@SuppressWarnings("unchecked")
		Map<String,String> map = JSONObject.parseObject(body, Map.class);
		if(map.containsKey("cacheBuster")) {
			map.remove("cacheBuster");
		}
		if(map.containsKey("crl_method")) {
			map.remove("crl_method");
		}
		ReturnResult result = commonService.updateCommonRequest(map);
		if(result.isSuccess()) {
			return ResponseEntity.ok(result);
		} else if(ErrorCode.PBE999.errorCode().equals(result.getErrorCode())) {
			//若是系统异常
			result.setErrorMsg(SYSTEM_ERROR);
			return new ResponseEntity<ReturnResult>(result, HttpStatus.INTERNAL_SERVER_ERROR);
		}
		//请求不成功
		return new ResponseEntity<ReturnResult>(result, HttpStatus.BAD_REQUEST);
		
	}
	/**
	 * 获取所有错误状态码
	 * @param <V>
	 * @return
	 */
	@RequestMapping(value="/getAllErrorCode",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, String> getAllErrorCode() {
		ErrorCode[] values = ErrorCode.values();
		Map<String,String> map = new HashMap<String, String>();
		for (ErrorCode errorCode : values) {
			map.put(errorCode.errorCode(), errorCode.errorMsg());
		}
		return map;
		
	}
}
